import 'package:dartx/dartx.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:rxdart/rxdart.dart' as rx;

import 'ble_state_view.dart';

class BleFutureView<T> extends StatelessWidget {
  final BleManager? bleManager;
  final Widget Function(T? data) builder;
  final Future<T>? Function(BleManager? manager) futureBuilder;
  final T? initialData;
  final Widget? disconnectedWidget;

  const BleFutureView(
      {super.key,
      this.bleManager,
      required this.builder,
      required this.futureBuilder,
      this.disconnectedWidget,
      this.initialData});

  @override
  Widget build(BuildContext context) {
    return BleStateView(
        bleManager: bleManager,
        builder: (bool connected) {
          if (connected) {
            return FutureBuilder<T>(
                future: futureBuilder(bleManager),
                initialData: initialData,
                builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
                  return builder(snapshot.data);
                });
          } else {
            return disconnectedWidget ?? Container();
          }
        });
  }
}
